# coding=utf-8
import os, json
import yaml
import configparser


class YmlUtil:

    @classmethod
    def readYml(cls, path, key, para=None):
        """
        path,key必填，para不传时会把key下面所有参数获取(返回字典类型)，para传值会返回参数对应的值
        """
        f = open(path, encoding='utf-8')
        res = yaml.load(f, Loader=yaml.FullLoader)
        if para is None:
            value = res.get(key)
        else:
            value = res.get(key)[para]
        f.close()
        return value

    @classmethod
    def writeYml(cls, path, key, para, value):
        """
        path,key必填，para不传时会把key下面所有参数获取(返回字典类型)，para传值会返回参数对应的值
        """
        res = ""
        with open(path, 'r', encoding='utf-8') as f1:
            res = yaml.load(f1, Loader=yaml.FullLoader)
            res[key][para] = value
      
        with open(path, 'r+', encoding='utf-8') as f:
             yaml.dump(res, f)

        return "数据更新成功"



class ConfigUtil(object):
    def __init__(self, path):
        self.path = path

        if not os.path.exists(self.path):
            raise IOError('file {} not found!'.format(self.path))
        try:
            self.cf = configparser.ConfigParser()
            self.cf.read(self.path)
        except Exception as e:
            raise IOError(str(e))

    def get(self, section, key):
        active_section = self.cf.get(section, key)
        return active_section
